<?php

namespace pmvc\controller;

use pmvc\Controller;
use pmvc\ModelAndView;

class BaseFormController
	implements Controller {
	
	private $formView;
	private $errorView;
	private $successView;
	private $errors = Array();
	
	protected function addGlobalError($message) {
		if (!array_key_exists("globalErrors", $this->errors)) {
			$this->errors["globalErrors"] = Array();
		}
		$this->errors["globalErrors"][] = $message;
	}
	
	protected function addFieldError($field, $message) {
		if (!array_key_exists("fieldErrors", $this->errors)) {
			$this->errors["fieldErrors"] = Array();
		}
		if (!array_key_exists($field, $this->errors["fieldErrors"])) {
			$this->errors["fieldErrors"][$field] = Array();
		}
		$this->errors["fieldErrors"][$field][] = $message;
	}
	
	protected function clearFieldErrors($field=FALSE) {
		if (!array_key_exists("fieldErrors", $this->errors)) {
			return;
		} else if ($field===FALSE) {
			$this->errors["fieldErrors"] = Array();
		} else {
			$this->errors["fieldErrors"][$field] = Array();
		}
	}
	
	protected function clearGlobalErrors() {
		if (!array_key_exists("globalErrors", $this->errors)) {
			return;
		} else {
			$this->errors["globalErrors"] = Array();
		}
	}
	
	protected function clearAllErrors() {
		$this->errors = Array();
	}
	
	protected function hasErrors() {
		if (array_key_exists("globalErrors", $this->errors)
			&& count($this->errors["globalErrors"])>0) {
			return true;
		} else if (array_key_exists("fieldErrors", $this->errors)) {
			foreach ($this->errors["fieldErrors"] as $fieldError) {
				if (count($fieldError)>0) {
					return true;
				}
			}
		}
		return false;
	}
	
	public function isFormSubmission() {
		return ($_SERVER["REQUEST_METHOD"]=="POST");
	}
	
	public function populateModel(&$model) {
		if ($this->hasErrors()) {
			$model["formErrors"] = $this->errors;
		}
		return;
	}
	
	public function showForm(&$model) {
		pmvc_logdebug("BaseFormController::showForm()");
		$this->populateModel($model);
		return new ModelAndView($this->formView, $model);
	}
	
	public function validateForm(&$model) {
		return TRUE;
	}
	
	public function showValidationError(&$model) {
		pmvc_logdebug("BaseFormController::showValidationError()");
		$this->populateModel($model);
		return new ModelAndView($this->errorView, $model);
	}
	
	public function processForm(&$model) {
		return TRUE;
	}
	
	public function showProcessError(&$model) {
		pmvc_logdebug("BaseFormController::showProcessError()");
		$this->populateModel($model);
		return new ModelAndView($this->errorView, $model);
	}
	
	public function showSuccess(&$model) {
		pmvc_logdebug("BaseFormController::showSuccess()");
		$this->populateModel($model);
		return new ModelAndView($this->successView, $model);
	}
	
	/**
	 * @see lib.pmvc.Controller#handleRequest()
	 */
    public final function handleRequest() {
    	
    	// create model
    	$model = Array();
    	
    	// send to formView
    	if (!$this->isFormSubmission()) {
    		return $this->showForm($model);
    		
    	// proccess form submission
    	} else if (!$this->validateForm($model)) {
    		return $this->showValidationError($model);
    			
    	// proccess form submission
    	} else if (!$this->processForm($model)) {
    		return $this->showProcessError($model);
    		
    	// submission was successful
    	} else {
    		return $this->showSuccess($model);
    	}
    }
	
	/**
	 * @see lib.pmvc.Controller#handleRequest()
	 */
    public function configure($conf) {
    	$this->formView 	= isset($conf["formView"]) ? $conf["formView"] : FALSE;
    	$this->errorView 	= isset($conf["errorView"]) ? $conf["errorView"] : FALSE;
    	$this->successView	= isset($conf["successView"]) ? $conf["successView"] : FALSE;
    }
		
}

?>